package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.Ebook;
import com.jiawa.wiki.domain.EbookExample;
import com.jiawa.wiki.domain.Testresult;
import com.jiawa.wiki.domain.TestresultExample;
import com.jiawa.wiki.mapper.TestresultMapper;
import com.jiawa.wiki.req.EbookQueryReq;
import com.jiawa.wiki.req.TestresultQueryReq;
import com.jiawa.wiki.resp.EbookQueryResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.resp.TestresultQueryResp;
import com.jiawa.wiki.util.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class TestResultService {
    private static final Logger LOG = LoggerFactory.getLogger(TestResultService.class);

    @Autowired
    private TestresultMapper testresultMapper;



    public PageResp<TestresultQueryResp> list(TestresultQueryReq req) {

        TestresultExample testresultExample = new TestresultExample();
        TestresultExample.Criteria criteria = testresultExample.createCriteria();
        if(!ObjectUtils.isEmpty(req.get序列号())){
            criteria.and序列号Like("%"+req.get序列号()+"%");
        }


        PageHelper.startPage( req.getPage(),req.getSize());
        List<Testresult> testresultList = testresultMapper.selectByExample(testresultExample);

        PageInfo<Testresult> pageInfo = new PageInfo<>(testresultList);
        LOG.info("总行数：{}",pageInfo.getTotal());
        LOG.info("总页数：{}",pageInfo.getPages());
//        List<EbookQueryResp> respList =  new ArrayList<>();
//        for (Ebook ebook : ebookList) {
//            EbookQueryResp ebookResp = new EbookQueryResp();
//            BeanUtils.copyProperties(ebook,ebookResp);
//            respList.add(ebookResp);
//        }

        List<TestresultQueryResp> respList = CopyUtil.copyList(testresultList, TestresultQueryResp.class);
        PageResp<TestresultQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }


}
